<?php
namespace app\daohang\event;

use think\Controller;

class Auth extends Controller
{
	
    public function _initialize()
    {
        parent::_initialize();
    }

	public function index()
    {
        //定义用户组
        $authOption = DcAuthOption();

        unset($authOption['administrator']);
        
        //字段列表
        $items  = [
            'auth_filter' => [
                'type'   => 'checkbox',
                'value'  => config('daohang.auth_filter'),
            ],
            'auth_search' => [
                'type'   => 'checkbox',
                'value'  => config('daohang.auth_search'),
            ],
            'auth_web_save' => [
                'type'   => 'checkbox',
                'value'  => config('daohang.auth_web_save'),
            ],
            'auth_mp_save' => [
                'type'   => 'checkbox',
                'value'  => config('daohang.auth_mp_save'),
            ],
            'auth_mini_save' => [
                'type'   => 'checkbox',
                'value'  => config('daohang.auth_mini_save'),
            ],
        ];
        
        foreach($items as $key=>$value){
            $items[$key]['option'] = $authOption;
            $items[$key]['title']  = lang('dh_'.$key);
            if(!isset($value['placeholder'])){
                $items[$key]['placeholder'] = lang('dh_'.$key.'_placeholder');
            }
            if(!isset($value['class_right_check'])){
                $items[$key]['class_right_check'] = 'form-check form-check-inline py-1';
            }
        }
        
        $this->assign('items', DcFormItems($items));
        
        return $this->fetch('daohang@auth/index');
	}
    
    public function update()
    {
        $post = [];
        $post['auth_filter']       = input('post.auth_filter/a');
        $post['auth_search']       = input('post.auth_search/a');
        $post['auth_web_save']     = input('post.auth_web_save/a');
        $post['auth_mp_save']      = input('post.auth_mp_save/a');
        $post['auth_mini_save']    = input('post.auth_mini_save/a');
        //批量保存配置（权限设置）
		if( !daohangConfigSave($post) ){
		    $this->error(lang('fail'));
        }
        //批量删除权限（统一由后台的权限管理接管,先清除导航插件的所有权限节点）
        $status = daohangConfigDelete(['op_controll'=>'auth']);
        //批量添加权限数据
        $authList = [];
        //初始权限字段
        $default = [
            'op_module'     => 'daohang',
            'op_controll'   => 'auth',
            'op_action'     => 'system',
            'op_order'      => 0,
            'op_status'     => 'normal',
        ];
        foreach($post['auth_filter'] as $key=>$group){
            array_push($authList,DcArrayArgs([
                'op_name'       => $group,
                'op_value'      => 'daohang/filter/index',
            ],$default));
        }
        foreach($post['auth_search'] as $key=>$group){
            array_push($authList,DcArrayArgs([
                'op_name'       => $group,
                'op_value'      => 'daohang/search/index',
            ],$default));
        }
        foreach($post['auth_web_save'] as $key=>$group){
            array_push($authList,DcArrayArgs([
                'op_name'       => $group,
                'op_value'      => 'daohang/publish/websave',
            ],$default));
        }
        foreach($post['auth_mp_save'] as $key=>$group){
            array_push($authList,DcArrayArgs([
                'op_name'       => $group,
                'op_value'      => 'daohang/publish/mpsave',
            ],$default));
        }
        foreach($post['auth_mini_save'] as $key=>$group){
            array_push($authList,DcArrayArgs([
                'op_name'       => $group,
                'op_value'      => 'daohang/publish/minisave',
            ],$default));
        }
        //批量添加权限节点
        \daicuo\Op::save_all($authList);
        //清空缓存
        DcCache('auth_all', null);
        //返回结果
        $this->success(lang('success'));
	}
	
}